<script setup>
import TheAvatar from "../components/TheAvatar.vue";
import TheIcon from "../components/TheIcon.vue";
import { ref, computed, watch } from "vue";
import { useStore } from "vuex";
const store = useStore();
const userAvatar = computed(() => store.state.user.userAvatar);

//用户名称
const userInfo = computed(() => {
  return store.state.user.user;
});

//tab栏更改active
const currentIndex = ref(0);
const tabs = ref([
  {
    label: "我的",
    icon: "posts",
  },
  {
    label: "赞过",
    icon: "like",
  },
  {
    label: "收藏",
    icon: "favorite",
  },
]);

//展示不同显示
const showList = ref([]);
const picList = computed(() => {
  return store.state.post.files;
});

watch(
  currentIndex,
  (newVal) => {
    if (newVal === 0) {
      showList.value = picList.value;
    } else if (newVal === 1) {
      showList.value = picList.value.filter((item) => item.like === true);
    } else if (newVal === 2) {
      showList.value = picList.value.filter((item) => item.favor === true);
    }
  },
  {
    immediate: true,
  }
);
</script>
<template>
  <div>
    <div class="profileContainer">
      <TheAvatar :src="userAvatar" :width="186" :height="186"></TheAvatar>
      <div class="profile">
        <p class="name">
          <span> {{ userInfo.username }}</span>
          <router-link to="/profile/edit">编辑个人资料</router-link>
        </p>
        <p class="handle">@{{ userInfo.name }}</p>
        <div class="description">
          <pre>{{ userInfo.intro }}</pre>
        </div>
        <p class="website">{{ userInfo.website }}</p>
      </div>
    </div>
    <div class="tabs">
      <div
        class="tab"
        v-for="(tab, index) in tabs"
        :key="index"
        :class="{ active: currentIndex === index }"
        @click="currentIndex = index"
      >
        <TheIcon :icon="tab.icon"></TheIcon>
        <p>{{ tab.label }}</p>
      </div>
    </div>
    <div class="tabContent">
      <p>{{ 162 }}篇帖子</p>
      <div class="posts">
        <div v-for="item in showList" class="temp" :key="item.id">
          <img class="postImage" :src="item.src" />
          <span>{{ item.id }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.profileContainer {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 24px;
}

.avatar {
  justify-self: end;
}

.profile .name {
  display: flex;
  align-items: center;
}

.profile .name > span {
  font-size: 26px;
}

.profile .name > a {
  color: #1da0ff;
  text-decoration: none;
  margin-left: 26px;
}

.profile .handle {
  margin-top: 4px;
  color: #848484;
}

.profile .description {
  margin-top: 26px;
  margin-bottom: 22px;
}

.tabs {
  display: grid;
  grid-template-columns: repeat(3, 88px);
  column-gap: 4vw;
  justify-content: center;
  margin-top: 7vmin;
  margin-bottom: 24px;
}

.tab {
  /* text-align: center; */
  padding: 12px 0;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.tab > svg {
  width: 50px;
  height: 50px;
  stroke: #8a9194;
  fill: #8a9194;
  transform: translateX(8px);
}

.tab.active {
  background-color: #f6f6f6;
  border-radius: 18px;
}

.tab.active > svg {
  stroke: #1787d9;
  fill: #1787d9;
}

.tab.active > p {
  color: #1787d9;
}

.tabContent p {
  text-align: center;
  /* margin: 0 auto; */
  /* padding: 0 auto; */
  /* margin-top: 8px; */
  margin-bottom: 24px;
  font-weight: 600;
}

.posts {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 24px;
}
.postImage {
  width: 100%;
  height: 320px;
  background-color: #eee;
  object-fit: cover;
  cursor: pointer;
}

.temp {
  position: relative;
}

.temp span {
  position: absolute;
  top: 0;
  left: 0;
  color: white;
  font-size: 20px;
  font-weight: 700;
}
</style>
